package cn.sccl.test.lazy_fetch;

import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import cn.sccl.domain.Customer;
import cn.sccl.domain.LinkMan;
import cn.sccl.utils.HibernateUtils;

/**
 * @author raoshuang
 * @date 2019年8月28日下午3:01:01
 * @version 1.0.0
 * 关联级别 延迟加载 & 抓取策略
 */
@SuppressWarnings("all")
public class LazyFetchDemo {
	@Test
	public void fun() {//准备数据
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		try {
			for (int i = 0; i < 3; i++) {
				Customer customer = new Customer();
				customer.setCust_name("腾讯"+i);
				
				LinkMan linkMan = new LinkMan();
				linkMan.setLkm_name("麻花"+i);
				linkMan.setCustomer(customer);
				
				session.save(customer);
				session.save(linkMan);
			}
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		}
		tx.commit();
		session.close();
	}
	
	//集合级别的关联
	//fetch:select 单表查询
	//lazy:true 使用时才加载集合数据.
	@Test
	public void fun1(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer c = session.get(Customer.class, 2l);
		Set<LinkMan> linkMens = c.getLinkMens();//关联级别
		
		tx.commit();
		System.out.println(linkMens);
		session.close();
	}

	//集合级别的关联
	//fetch:select 单表查询
	//lazy:false 立即记载集合数据
	@Test
	public void fun2(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer c = session.get(Customer.class, 2l);
		Set<LinkMan> linkMens = c.getLinkMens();//关联级别
		
		System.out.println(linkMens);
		
		tx.commit();
		session.close();
	}
	//集合级别的关联
	//fetch:select 单表查询
	//lazy:extra 极其懒惰.与懒加载效果基本一致. 如果只获得集合的size.只查询集合的size(count语句)
	@Test
	public void fun3(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer c = session.get(Customer.class, 2l);
		Set<LinkMan> linkMens = c.getLinkMens();//关联级别
		
		System.out.println(linkMens.size());
		System.out.println(linkMens);
		
		tx.commit();
		session.close();
	}
	//集合级别的关联
	//fetch:join	多表查询
	//lazy:true|false|extra 失效.立即加载.
	@Test
	public void fun4(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer c = session.get(Customer.class, 2l);
		Set<LinkMan> linkMens = c.getLinkMens();//关联级别
		
		System.out.println(linkMens.size());
		System.out.println(linkMens);
		
		tx.commit();
		session.close();
	}
	
	@Test
	//fetch: subselect 子查询
	//lazy: true 懒加载
	public void fun5(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
			
		String  hql = "from Customer";
		Query query = session.createQuery(hql);
		
		List<Customer> list = query.list();
		for(Customer c:list){
			System.out.println(c);
			System.out.println(c.getLinkMens().size());
			System.out.println(c.getLinkMens());
		}
		
		tx.commit();
		session.close();
	}
	@Test
	//fetch: subselect 子查询
	//lazy: false 立即加载
	public void fun6(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
			
		String  hql = "from Customer";
		Query query = session.createQuery(hql);
		
		List<Customer> list = query.list();
		for(Customer c:list){
			System.out.println(c);
			System.out.println(c.getLinkMens().size());
			System.out.println(c.getLinkMens());
		}
		
		tx.commit();
		session.close();
	}
	
	@Test
	//fetch: subselect 子查询
	//lazy: extra 极其懒惰
	public void fun7(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
			
		String  hql = "from Customer";
		Query query = session.createQuery(hql);
		
		List<Customer> list = query.list();
		for(Customer c:list){
			System.out.println(c);
			System.out.println(c.getLinkMens().size());
			System.out.println(c.getLinkMens());
		}
		
		tx.commit();
		session.close();
	}
}
